package com.systop.party08.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.material.tabs.TabLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.systop.party08.R;
import com.systop.party08.adapter.FragPagerAdapter;
import com.systop.party08.base.BaseFragment;
import com.systop.party08.entity.HomeEntity;
import com.systop.party08.entity.TabVpEntity;
import com.systop.party08.http.MyStringCallback;
import com.systop.party08.utils.GeneralTools;
import com.systop.party08.utils.SPUtils;
import com.systop.party08.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.systop.party08.http.ComUrl.INDEX;
import static com.systop.party08.utils.StaticDateUtils.SP_TOKEN;

/**
 * 首页
 */
public class HomeFragment extends BaseFragment {

    @BindView(R.id.home_tab)
    TabLayout homeTab;
    @BindView(R.id.home_vp)
    ViewPager homeVp;
    @BindView(R.id.home_refresh)
    SmartRefreshLayout refresh;

    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);
        refresh.setEnableLoadMore(false);
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                okhttpIndex();
            }
        });
        refresh.autoRefresh();
        return view;
    }

    private void okhttpIndex() {
        String response="{\"msg\":\" 请求成功\",\"data\":{\"lmList\":[{\"name\":\"政策文件\",\"id\":6},{\"name\":\"活动证管理\",\"id\":12},{\"name\":\"服务指南\",\"id\":16},{\"name\":\"风采展示\",\"id\":21},{\"name\":\"党员学习之友\",\"id\":27},{\"name\":\"网上公示\",\"id\":22},{\"name\":\"党建动态\",\"id\":17},{\"name\":\"新闻动态\",\"id\":36},{\"name\":\"最新\",\"id\":40}],\"jdwzList\":[{\"focusPic\":\"ds.jpg\",\"id\":851,\"title\":\"学党史 明德致知\",\"pubDate\":1594310400000},{\"focusPic\":\"zy.png\",\"id\":825,\"title\":\"战“疫”进行时 我是中共党员我承诺\",\"pubDate\":1580745600000},{\"focusPic\":\"gdfys.png\",\"id\":826,\"title\":\"人民日报和音：正是共担风雨时\",\"pubDate\":1580745600000}],\"wzList\":[{\"data\":[{\"title\":\"关于2020年度“七一”表彰活动的通知\",\"id\":256,\"focusPic\":null,\"pubDate\":\"2020-06-26\"},{\"title\":\"系统升级通知\",\"id\":254,\"focusPic\":null,\"pubDate\":\"2020-06-20\"},{\"title\":\"不急事项延后办、紧急事项预约办！省人才交流服务中心发布倡议书\",\"id\":253,\"focusPic\":null,\"pubDate\":\"2020-06-19\"},{\"title\":\"关于在肺炎疫情防控工作中充分发挥基层党组织战斗堡垒作用和党员先锋模范作用的倡议书\",\"id\":252}],\"lnmc\":\"最新通知\"},{\"data\":[{\"focusPic\":null,\"id\":828,\"title\":\"习近平主持召开中央全面深化改革委员会第十二次会议\",\"pubDate\":\"2020-02-14\"},{\"focusPic\":null,\"id\":826,\"title\":\"人民日报和音：正是共担风雨时\",\"pubDate\":\"2020-02-04\"},{\"focusPic\":null,\"id\":830,\"title\":\"习近平：在中央政治局常委会会议研究应对新型冠状病毒肺炎疫情工作时的讲话\",\"pubDate\":\"2020-02-03\"},{\"focusPic\":null,\"id\":763,\"title\":\"“冀才杯”河北省第九届流动党员运动会举行\",\"pubDate\":\"2018-05-14\"}],\"lnmc\":\"党建活动\"},{\"data\":[{\"focusPic\":null,\"id\":841,\"title\":\"习近平给袁隆平、钟南山、叶培建等25位科技工作者代表的回信\",\"pubDate\":\"2020-05-29\"},{\"focusPic\":null,\"id\":842,\"title\":\"习近平“2020两会时间”不寻常\",\"pubDate\":\"2020-05-29\"},{\"focusPic\":null,\"id\":839,\"title\":\"习近平出席解放军和武警部队代表团全体会议\",\"pubDate\":\"2020-05-27\"},{\"focusPic\":null,\"id\":840,\"title\":\"两会内外，习近平这样谈全面依法治国\",\"pubDate\":\"2020-05-27\"}],\"lnmc\":\"新闻动态\"},{\"data\":[{\"focusPic\":null,\"id\":707,\"title\":\"【两学一做】中国共产党党内监督条例（全文）\",\"pubDate\":\"2017-11-27\"},{\"focusPic\":null,\"id\":708,\"title\":\"【两学一做】中共中央印发《县以上党和国家机关党员领导干部民主生活会若干规定》\",\"pubDate\":\"2017-11-27\"},{\"focusPic\":null,\"id\":710,\"title\":\"【两学一做】习近平：决胜全面建成小康社会 夺取新时代中国特色社会主义伟大胜利\",\"pubDate\":\"2017-11-27\"},{\"focusPic\":null,\"id\":711,\"title\":\"【两学一做】中国共产党廉洁自律准则\",\"pubDate\":\"2017-11-27\"}],\"lnmc\":\"两学一做\"},{\"data\":[{\"focusPic\":null,\"id\":814,\"title\":\"2019年转正名单\",\"pubDate\":\"2020-01-02\"},{\"focusPic\":null,\"id\":802,\"title\":\"关于2019年慰问生活困难党员的公示\",\"pubDate\":\"2019-01-25\"},{\"focusPic\":null,\"id\":801,\"title\":\"2018年转正名单\",\"pubDate\":\"2019-01-24\"},{\"focusPic\":null,\"id\":748,\"title\":\"2017年第四批转正人员名单\",\"pubDate\":\"2018-03-09\"}],\"lnmc\":\"网上公示\"},{\"data\":[{\"focusPic\":null,\"id\":722,\"title\":\"【十九大】习近平：决胜全面建成小康社会 夺取新时代中国特色社会主义伟大胜利——在中国共产党第十九次全国代表大会上的报告\",\"pubDate\":\"2017-10-27\"},{\"focusPic\":null,\"id\":723,\"title\":\"【十九大】中国共产党第十九届中央委员会候补委员名单\",\"pubDate\":\"2017-10-24\"},{\"focusPic\":null,\"id\":724,\"title\":\"【十九大】中国共产党第十九次全国代表大会关于《中国共产党章程（修正案）》的决议\",\"pubDate\":\"2017-10-24\"},{\"focusPic\":null,\"id\":725,\"title\":\"【十九大】中国共产党第十九次全国代表大会关于十八届中央纪律检查委员会工作报告的决议\",\"pubDate\":\"2017-10-24\"}],\"lnmc\":\"十九大汇编\"},{\"data\":[{\"focusPic\":null,\"id\":786,\"title\":\"庆祝改革开放40周年大会心得体会评优结果\",\"pubDate\":\"2019-01-10\"},{\"focusPic\":null,\"id\":787,\"title\":\"不忘初心，砥砺前行(曹彦鹏)\",\"pubDate\":\"2019-01-10\"},{\"focusPic\":null,\"id\":788,\"title\":\"《我们一起走过致敬改革开放40周年》(范锦地)\",\"pubDate\":\"2019-01-10\"},{\"focusPic\":null,\"id\":789,\"title\":\"我和我的祖国(葛川蕾)\",\"pubDate\":\"2019-01-10\"}],\"lnmc\":\"党员文摘\"}]},\"status\":\"200\"}";
        HashMap<String, String> param = new HashMap<>();
        HomeEntity entity = gson.fromJson(response, HomeEntity.class);
        setView(entity);

    }

    private void setView(HomeEntity entity) {
        if (entity != null && entity.getData() != null) {
            HomeEntity.DataBeanX dataBeanX = entity.getData();
            List<HomeEntity.DataBeanX.LmListBean> lmList = dataBeanX.getLmList();
            if (lmList != null && lmList.size() > 0) {
                ArrayList<TabVpEntity> mTabVps = new ArrayList<>();
                mTabVps.add(new TabVpEntity("首页", HomeHomeFragment.newInstance(entity.getData())));
                for (HomeEntity.DataBeanX.LmListBean item : lmList) {
                    mTabVps.add(new TabVpEntity(item.getName(), HomeArticleFragment.newInstance(item.getId())));
                }
                FragPagerAdapter adapter = new FragPagerAdapter(getActivity().getSupportFragmentManager(), mTabVps);
                homeVp.setAdapter(adapter);
                homeTab.setupWithViewPager(homeVp);
                homeVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                        ToastUtils.getInstance(getActivity()).showMessage("当前为第"+position+"个Tab");
                    }

                    @Override
                    public void onPageSelected(int position) {
                        if (position == 0) {
                            refresh.setEnableLoadMore(false);
                            refresh.setEnableRefresh(true);
                        } else {
                            refresh.setEnableLoadMore(false);
                            refresh.setEnableRefresh(false);
                        }
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });
            }
        }
    }


}